import os.path
#ecoding: utf-8 -*-
from quixote import get_request
from quixote import get_response
#from Cheetah.Template import Template
from zope.pagetemplate.pagetemplatefile import PageTemplateFile
from pynixweb.config import config
import os
dirs_config = config['dirs_config']

#def render_template(templateFile, nameSpace={}):
#    nameSpace["request"] = get_request()
#    nameSpace["response"] = get_response()
#    t = Template(file=os.path.join(config['templateRoot'], templateFile), compilerSettings={'prioritizeSearchListOverSelf':True},
#                 searchList=[nameSpace])
#    return t.__str__()

commonsDir = os.path.join(dirs_config['template_root'], 'commons')
commonsDirs = os.listdir(commonsDir)
commons = {}
for oneFile in commonsDirs:
        if not os.path.isdir(oneFile):
            one = PageTemplateFile(oneFile, commonsDir)
            commons[oneFile.split('.')[0]] = one

def render_template(templateFile, nameSpace={}):
    nameSpace["request"] = get_request()
    nameSpace["response"] = get_response()
    nameSpace["commons"] = commons
    t = PageTemplateFile(filename=templateFile, _prefix=dirs_config['template_root'])
    return t.pt_render(nameSpace);
